/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taskmanager2ndedition.client;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import taskmanager2ndedition.ClientTaskInfo;

/**
 *
 * @author denaas
 */
public class ClientTaskManager {
    private List<ClientTaskInfo> list = new LinkedList<ClientTaskInfo>();
    private ClientController controller;
    private Timer timer;

    private class UpdateTaskList extends TimerTask{

        @Override
        public void run() {
            List<ClientTaskInfo> updateList = controller.updateTasks();
            if (updateList == null || updateList.isEmpty())
                return;
            for(ClientTaskInfo task: updateList){
                switch(task.getState()){
                    case Added:
                        if (findById(task.getId()) == null){
                            add(task);
                        }
                        break;
                    case Editted:
                        replace(task);
                        break;
                    case Removed:
                        remove(task);
                        break;
                }
            }
            controller.setTaskListModified(true);
        }
        
    }
    
    
    
    public ClientTaskManager(ClientController controller){
        this.controller = controller;        
        forceUpdate();        
        
        timer = new java.util.Timer();
        timer.scheduleAtFixedRate(new UpdateTaskList(), new Date(), controller.getUpdatePeriod());
    }
    
    public void finalize(){
        timer.cancel();
    }

    public void add(ClientTaskInfo task) {
        if (task == null)
            return;
        list.add(task);
    }

    public void replace(ClientTaskInfo task) {
        if (task == null)
            return;
        ClientTaskInfo taskToRemove = findById(task.getId());
        remove(taskToRemove);
        add(task);
    }

    public void remove(ClientTaskInfo task) {
        if (task == null)
            return;
        ClientTaskInfo taskToRemove = findById(task.getId());
        list.remove(taskToRemove);
    }

    public List<ClientTaskInfo> getTasks() {
        return list;
    }
    
    public void forceUpdate() {
        list.clear();
        controller.setTaskListModified(true);
        List<ClientTaskInfo> updateList = controller.getAllTasks();
        if (updateList == null || updateList.isEmpty())
            return;
        for(ClientTaskInfo task: updateList){
            switch(task.getState()){
                case Added:
                    if (findById(task.getId()) == null){
                            add(task);
                    }
                    break;
                case Editted:
                    replace(task);
                    break;
                case Removed:
                    remove(task);
                    break;
            }
        }        
    }

    public List<ClientTaskInfo> getTasksOnDate(java.util.Date date) {
        Calendar cl = new GregorianCalendar();
        cl.setTime(date);
        cl.set(Calendar.HOUR_OF_DAY, 0);
        cl.set(Calendar.MINUTE, 0);
        cl.set(Calendar.SECOND, 0);
        cl.set(Calendar.MILLISECOND, 0);
        Date d1 = (java.util.Date) cl.getTime();
        cl.add(Calendar.DAY_OF_YEAR, 1);
        Date d2 = (java.util.Date) cl.getTime();

        List<ClientTaskInfo> tasksToDate = new LinkedList<ClientTaskInfo>();
        for (ClientTaskInfo task : list) {
            java.util.Date theDate = task.getExpirationDate();
            if (theDate.after(d1) && theDate.before(d2)) {
                tasksToDate.add(task);
            }
        }
        return tasksToDate;
    }

//------------------------------------------------------------------------------
    public ClientTaskInfo findById(int id) {
        for (ClientTaskInfo task: list){
            if (task.getId() == id)
                return task;
        }
        
        return null;
    }
    
}
